const {TouchBar} = require('electron')

const {TouchBarLabel, TouchBarButton, TouchBarSpacer} = TouchBar

//是否正在运行
let spinning = false
//金币数量
let money = 100;
//已经初始化过了(已经执行过一次)
let initialized = false;

// Reel labels
const reel1 = new TouchBarLabel()
const reel2 = new TouchBarLabel()
const reel3 = new TouchBarLabel()

//结果文本的 label
const result = new TouchBarLabel()

//分数
const source = new TouchBarLabel();
// source.label = "💰 × " + money;


//金币减少或增加的动画时间
const SHOW_TIME = 800;
//增加金币的时间间隔
const ADD_MONEY_TIME = 60 * 1000;
//一次增加金币的数量
const ONCE_ADD_MONEY_COUNT = 1;
//金币最大数量
const MAX_MONEY_NUMBER = 999;


//设置定时器,每分钟增加一个金币
setInterval(() => {
    //设置最大金币数为999
    if (money >= MAX_MONEY_NUMBER) {
        return;
    }
    money += ONCE_ADD_MONEY_COUNT;
    if (initialized) {
        source.label = "💰 × " + money;
    }
}, ADD_MONEY_TIME);


// 欢乐老虎机 按钮
const spin = new TouchBarButton({
    label: '🎰 欢乐老虎机',
    backgroundColor: '#7851A9',
    click: () => {
        //点击检查,如果正在执行则跳过
        if (spinning) {
            return
        }

        //判断是否还有金币
        if (money < 10) {
            //没有金币了
            result.label = '金币不足,充钱!!'
            return;
        }

        //开始
        spinning = true
        initialized = true;
        result.label = ''

        //扣金币开始,
        money -= 10;
        source.label = "💰 - 10";
        setTimeout(() => {
            source.label = "💰 × " + money;
        }, SHOW_TIME);


        let timeout = 10
        const spinLength = 4 * 1000 // 4 seconds
        const startTime = Date.now()

        const spinReels = () => {
            updateReels()

            if ((Date.now() - startTime) >= spinLength) {
                finishSpin()
            } else {
                // Slow down a bit on each spin
                timeout *= 1.1
                setTimeout(spinReels, timeout)
            }
        }

        spinReels()
    }
})

const getRandomValue = () => {
    const values = ['🍒', '💎', '7️⃣', '🍊', '🔔', '⭐', '🍇', '🍀']
    return values[Math.floor(Math.random() * values.length)]
}

const updateReels = () => {
    reel1.label = getRandomValue()
    reel2.label = getRandomValue()
    reel3.label = getRandomValue()
}

const finishSpin = () => {
    const uniqueValues = new Set([reel1.label, reel2.label, reel3.label]).size
    if (uniqueValues === 1) {
        //三个一样的,头彩
        result.label = '💰 头彩!'
        result.textColor = '#FDFF00'
        money += 20;
        source.label = "💰 + 20";
    } else if (uniqueValues === 2) {
        // 两个一样的
        result.label = '😍 可以呦!'
        result.textColor = '#FDFF00'
        money += 10;
        source.label = "💰 + 10";
    } else {
        //每一个都不一样
        result.label = '🙁 很遗憾~~'
        result.textColor = null
    }

    //设置分数,延时0.5秒,为上面的显示增加做准备
    setTimeout(() => {
        source.label = "💰 × " + money;
    }, SHOW_TIME);

    //游戏结束,释放锁
    spinning = false
}

const touchBar = new TouchBar({
    items: [
        spin,
        new TouchBarSpacer({size: 'large'}),
        reel1,
        new TouchBarSpacer({size: 'small'}),
        reel2,
        new TouchBarSpacer({size: 'small'}),
        reel3,
        new TouchBarSpacer({size: 'large'}),
        source,
        new TouchBarSpacer({size: 'large'}),
        result
    ]
})

module.exports = touchBar;